--    Author    : MAYANTING
--    Name      : ADM.RPT_SUST_DLMG_PAST_PRO_FLOAT_LOAF.HQL
--    Functions : 表29：贷款发生额分浮动方式分产品统计表
--    Purpose   :
--    Revisions or Comments
--    VER        DATE        AUTHOR           DESCRIPTION
--   ---------  ----------  ---------------  ------------------------------------
--    1.0        2018-06-11  MAYANTING           1.CREATE THE PROCEDURE
--
INSERT OVERWRITE TABLE ADM.RPT_SUST_DLMG_PAST_PRO_FLOAT_LOAF PARTITION (DATA_DATE = '#V_DATA_DATE#')
SELECT  AREA.AREA_CODE_4                 AS FIN_ORG_DIST
       ,T.CCY                            AS CCY
       ,T.IF_FLOAT                       AS IF_FLOAT        -- 利率是否固定
       ,T.FLOAT_DESC                     AS FLOAT_DESC      -- 利率类型描述
       ,T.BUSINESS_TYPE                  AS BUSINESS_TYPE   -- 贷款产品类型
       ,T.TYPE_DESC                      AS TYPE_DESC       -- 产品类型描述
       ,SUM(T.OCCUR_AMOUNT)/100000000    AS OCCUR_AMOUNT    -- 贷款发生额
       ,SUM(T.AMOUNT_LM    )/100000000   AS AMOUNT_LM       -- 发生额上期末
       ,SUM(T.AMOUNT_YS    )/100000000   AS AMOUNT_YS       -- 当年累计发生额
       ,SUM(T.AMOUNT_GROW  )/100000000   AS AMOUNT_GROW     -- 同比增长
       ,SUM(T.WSUM_AMOUNT  )/100000000   AS WSUM_AMOUNT     -- 发生额加权值
       ,SUM(T.WAMOUNT_LM   )/100000000   AS WAMOUNT_LM      -- 发生额加权值上期末
       ,SUM(T.WAMOUNT_YS   )/100000000   AS WAMOUNT_YS      -- 发生额加权值当年累计
       ,SUM(T.WAMOUNT_LC   )/100000000   AS WAMOUNT_LC      -- 发生额加权值去年同期
FROM (
    -- 消费贷款
    SELECT
     FSE.FIN_ORG_NO                                                                                   AS FIN_ORG_NO
    ,FSE.CCY                                                                                          AS CCY
    ,FSE.IF_FLOAT                                                                                     AS IF_FLOAT         -- 利率是否固定
    ,CASE WHEN FSE.IF_FLOAT='RF01' THEN '固定利率'
          WHEN FSE.IF_FLOAT='RF02' THEN '浮动利率'
     END                                                                                              AS FLOAT_DESC       -- 利率是否固定描述
    ,CASE -- 普通贷款
          WHEN FSE.BUSINESS_TYPE IN ('F0211','F0212','F02131','F02132','F0219') THEN 'A1'
          WHEN FSE.BUSINESS_TYPE IN ('F02201','F02202','F02203') THEN 'A2'
          WHEN FSE.BUSINESS_TYPE='F023' THEN 'A3'
          -- 贸易融资
          WHEN FSE.BUSINESS_TYPE='F081' THEN 'B1'
          WHEN FSE.BUSINESS_TYPE='F082' THEN 'B2'
          -- 融资租赁
           WHEN FSE.BUSINESS_TYPE='F09' THEN 'C'
     END                                                                                              AS BUSINESS_TYPE    -- 贷款产品类型
    ,CASE -- 普通贷款
          WHEN FSE.BUSINESS_TYPE IN ('F0211','F0212','F02131','F02132','F0219') THEN '消费贷款'
          WHEN FSE.BUSINESS_TYPE IN ('F02201','F02202','F02203') THEN '经营贷款'
          WHEN FSE.BUSINESS_TYPE='F023' THEN '固定资产贷款'
          -- 贸易融资
          WHEN FSE.BUSINESS_TYPE='F081' THEN '国际贸易融资'
          WHEN FSE.BUSINESS_TYPE='F082' THEN '国内贸易融资'
          -- 融资租赁
          WHEN FSE.BUSINESS_TYPE='F09' THEN '融资租赁'
     END                                                                                               AS TYPE_DESC        -- 贷款产品类型描述
    ,COALESCE(FSE.OCCUR_AMOUNT,0)                                                                      AS OCCUR_AMOUNT     -- 贷款发生额
    ,COALESCE(FSE.AMOUNT_LM,0)                                                                         AS AMOUNT_LM        -- 发生额上期末
    ,COALESCE(FSE.AMOUNT_YS,0)                                                                         AS AMOUNT_YS        -- 当年累计发生额
    ,COALESCE(FSE.OCCUR_AMOUNT,0) - COALESCE(FSE.AMOUNT_YC,0)                                          AS AMOUNT_GROW      -- 同比增减
    ,COALESCE(FSE.WSUM_AMOUNT,0)                                                                       AS WSUM_AMOUNT      -- 发生额加权值
    ,COALESCE(FSE.WAMOUNT_LM,0)                                                                        AS WAMOUNT_LM       -- 发生额加权值上期末
    ,COALESCE(FSE.WAMOUNT_YS,0)                                                                        AS WAMOUNT_YS       -- 发生额加权值当年累计
    ,COALESCE(FSE.WAMOUNT_YC,0)                                                                        AS WAMOUNT_LC       -- 发生额加权值去年同期期
    FROM (SELECT * FROM EDW.DS_LOAF_SUM WHERE BUSINESS_TYPE IN ('F0211','F0212','F02131','F02132','F0219','F02201','F02202','F02203','F023','F081','F082','F09')
          AND DATA_DATE='#V_DATA_DATE#') FSE

    UNION ALL

    -- 合计
    SELECT
     FSE.FIN_ORG_NO                                                          AS FIN_ORG_NO
    ,FSE.CCY                                                                 AS CCY
    ,FSE.IF_FLOAT                                                            AS IF_FLOAT         -- 利率是否固定
    ,CASE WHEN FSE.IF_FLOAT='RF01' THEN '固定利率'
          WHEN FSE.IF_FLOAT='RF02' THEN '浮动利率'
     END                                                                     AS FLOAT_DESC       -- 利率是否固定描述
    ,'Z'                                                                     AS BUSINESS_TYPE    -- 贷款产品类型
    ,'合计'                                                                  AS TYPE_DESC        -- 贷款产品类型描述
    ,COALESCE(FSE.OCCUR_AMOUNT,0)                                            AS OCCUR_AMOUNT     -- 贷款发生额
    ,COALESCE(FSE.AMOUNT_LM,0)                                               AS AMOUNT_LM        -- 发生额上期末
    ,COALESCE(FSE.AMOUNT_YS,0)                                               AS AMOUNT_YS        -- 当年累计发生额
    ,COALESCE(FSE.OCCUR_AMOUNT,0) - COALESCE(FSE.AMOUNT_YC,0)                AS AMOUNT_GROW      -- 同比增减
    ,COALESCE(FSE.WSUM_AMOUNT,0)                                             AS WSUM_AMOUNT      -- 发生额加权值
    ,COALESCE(FSE.WAMOUNT_LM,0)                                              AS WAMOUNT_LM       -- 发生额加权值上期末
    ,COALESCE(FSE.WAMOUNT_YS,0)                                              AS WAMOUNT_YS       -- 发生额加权值当年累计
    ,COALESCE(FSE.WAMOUNT_YC,0)                                              AS WAMOUNT_LC       -- 发生额加权值去年同期
    FROM (SELECT * FROM EDW.DS_LOAF_SUM WHERE DATA_DATE='#V_DATA_DATE#') FSE
  )T
 -- 与地区表最细级关联
 LEFT JOIN (SELECT * FROM DIMENSION.T_ORG_AREA_RELATION WHERE AREA_LEVEL='3') REAL ON T.FIN_ORG_NO=REAL.ORG_ID AND '#V_DATA_DATE#' BETWEEN REAL.START_DATE AND REAL.END_DATE
 LEFT JOIN DIMENSION.T_AREA_CODE AREA ON REAL.AREA_CODE=AREA.AREA_CODE_4 AND '#V_DATA_DATE#' BETWEEN AREA.START_DATE AND AREA.END_DATE
 GROUP BY   AREA.AREA_CODE_4
           ,T.CCY
           ,T.IF_FLOAT
           ,T.FLOAT_DESC
           ,T.BUSINESS_TYPE
           ,T.TYPE_DESC ;